import {reqcate,reqcategoods} from '../../request/api'
//初始状态
const initState={
        cateslist:[],//侧边栏
        
        categoods:[],//分类商品
        n:1
}

//TYPES
const TYPES={
        //侧边栏
        CHANGE_CATESLIST:'CHANGE_CATESLIST',
        CHANGE_CATESLIST_N:'CHANGE_CATESLIST_N',
        // 分类商品
        CHANGE_CATEGOODS:'CHANGE_CATEGOODS'
}

//reducer
const reducer =(state=initState,action)=>{
        switch(action.type){
                case TYPES.CHANGE_CATESLIST:
                        return{
                                ...state,
                                cateslist:action.cateslist
                        }
                case TYPES.CHANGE_CATESLIST_N:
                        return{
                                ...state,
                                n:action.n
                        }
                case TYPES.CHANGE_CATEGOODS:
                        return{
                                ...state,
                                categoods:action.categoods
                        }
                default:
                        return state
        }
}

//creators 做逻辑 方法  请求
export const actions={
        //分类侧边栏
        changeCateList:cateslist=>({type:TYPES.CHANGE_CATESLIST,cateslist}),
        //请求侧边栏数据
        requestCateList:()=>(dispatch,getState)=>{
                reqcate().then(res=>{
                        if(res.data.code==200){
                                dispatch(actions.changeCateList(res.data.list))
                        }
                })
        },
        // changeN:(n)=>({type:TYPES.CHANGE_CATESLIST_N,n}),
        // 修改n
        changeN:(n)=>(dispatch,getState)=>{ 
                // let n=getN(getState())
                reqcategoods({fid:n}).then(res=>{
                        if(res.data.code==200){
                                dispatch(actions.changeCateGoods(res.data.list))
                        }
                })
               return {type:TYPES.CHANGE_CATESLIST_N,n}
        },
        // 分类商品
        changeCateGoods:categoods=>({type:TYPES.CHANGE_CATEGOODS,categoods}),
        // //请求分类商品
        // requestcateGoods:()=>(dispatch,getState)=>{
        //         let n=getN(getState());
                
        //         reqcategoods({fid:n}).then(res=>{
        //                 if(res.data.code==200){
        //                         dispatch(actions.changeCateGoods(res.data.list))
        //                 }
        //         })
        // }

}

// 导出数据给组件
export const getcatelist=state=>state.cate.cateslist
export const getcateGoods=state=>state.cate.categoods

export const getN=state=>state.cate.n
// 导出reducer
export default reducer